package com.pig4cloud.pig.admin.api.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.ToString;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Data
@ToString
@TableName("news_list")
public class NewsList implements Serializable {
	private static final long serialVersionUID = 1L;

	@TableId(type = IdType.NONE)
	private Long id;

	@TableField("title")
	private String title;

	@TableField("sub_title")
	private String subTitle;

	@TableField("content")
	private String content;

	@TableField("url")
	private String url;

	@TableField("img_url")
	private String imgUrl;

	@TableField("create_time")
	private Date createTime;

	@TableField("insert_time")
	private Date insertTime;

	@TableField("del_flag")
	private String delFlag;

	@TableField("category")
	private String category;

	@TableField("url_hash")
	private Integer urlHash;

	@TableField(exist = false)
	private String dateRange;

	public static NewsList create(Long id) {
		NewsList newsList = new NewsList();
		newsList.setId(id);
		newsList.setInsertTime(new Date());
		return newsList;
	}

	public static Map<String, Object> toMap(NewsList news) {
		Map<String, Object> map = new HashMap<>();
		map.put("id", news.getId());
		map.put("title", news.getTitle());
		map.put("subTitle", news.getSubTitle());
		map.put("url", news.getUrl());
		map.put("imgUrl", news.getImgUrl());
		map.put("category", news.getCategory());
		map.put("createTime", news.getCreateTime());
		map.put("insertTime", news.getInsertTime());
		return map;
	}

	public static NewsList fromMap(Map<String, Object> map) {
		NewsList news = new NewsList();
		news.setId(Long.valueOf((String) map.get("id")));
		news.setTitle((String) map.get("title"));
		news.setSubTitle((String) map.get("subTitle"));
		news.setUrl((String) map.get("url"));
		news.setImgUrl((String) map.get("imgUrl"));
		news.setCategory((String) map.get("category"));
		news.setCreateTime((Date) map.get("createTime"));
		news.setInsertTime((Date) map.get("insertTime"));
		return news;
	}

	/*public String serialize() {
		return String.join("✂",
				id == null ? 0L : id,
				title == null ? "" : title,
				subTitle == null ? "" : subTitle,
				content == null ? "" : content,
				url == null ? "" : url,
				imgUrl == null ? "" : imgUrl,
				createTime == null ? "" : String.valueOf(createTime.getTime()),
				delFlag == null ? "" : delFlag,
				category == null ? "" : category,
				urlHash == null ? "" : String.valueOf(urlHash)
		);
	}*/


	// 反序列化方法
	public static NewsList deserialize(String serialized) {
		String[] parts = serialized.split("✂");
		NewsList newsList = new NewsList();
		if (parts.length > 0) newsList.setId(parts[0].isEmpty() ? null : Long.valueOf(parts[0]));
		if (parts.length > 1) newsList.setTitle(parts[1].isEmpty() ? null : parts[1]);
		if (parts.length > 2) newsList.setSubTitle(parts[2].isEmpty() ? null : parts[2]);
		if (parts.length > 3) newsList.setContent(parts[3].isEmpty() ? null : parts[3]);
		if (parts.length > 4) newsList.setUrl(parts[4].isEmpty() ? null : parts[4]);
		if (parts.length > 5) newsList.setImgUrl(parts[5].isEmpty() ? null : parts[5]);
		if (parts.length > 6) newsList.setCreateTime(parts[6].isEmpty() ? null : new Date(Long.parseLong(parts[6])));
		if (parts.length > 7) newsList.setDelFlag(parts[7].isEmpty() ? null : parts[7]);
		if (parts.length > 8) newsList.setCategory(parts[8].isEmpty() ? null : parts[8]);
		if (parts.length > 9) newsList.setUrlHash(parts[9].isEmpty() ? null : Integer.parseInt(parts[9]));
		return newsList;
	}

}

